package repository.impl;

import Util.JDBCtool;
import com.mysql.cj.jdbc.JdbcConnection;
import entity.Reader;
import repository.ReaderRepository;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class ReaderRepositoryImpl implements ReaderRepository {
    @Override
    public Reader login(String username, String password) throws SQLException {
        Connection connection= JDBCtool.getConnection();
        String sql="select * from reader where username =? and password =?";
        ResultSet resultSet=null;
        Reader reader=null;
        PreparedStatement statement=null;
        statement=connection.prepareStatement(sql);
        statement.setString(1,username);
        statement.setString(2,password);
        resultSet =statement.executeQuery();
        if (resultSet.next()){
            reader=new Reader(resultSet.getInt(1),resultSet.getString(2),resultSet.getString(3),resultSet.getString(4),resultSet.getString(5),resultSet.getString(6),resultSet.getString(7));
        }
        JDBCtool.release(connection,statement,resultSet);
        return reader;
    }
}
